package cn.itcast.bos.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.base.AreaService;
import cn.itcast.bos.util.PinYin4jUtils;

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class AreaAction extends BaseAction<Area>{
	
	private File file;

	public void setFile(File file) {
		this.file = file;
	}

	@Autowired
	private AreaService areaService;
	
	@Action(value="areaUploadAction")
	public String save() throws IOException{
		HSSFWorkbook hssfworkbook = new HSSFWorkbook(new FileInputStream(file));
		//得到第一张sheet表
		HSSFSheet sheet = hssfworkbook.getSheetAt(0);
		//循环获得每一行
		List<Area> areas = new ArrayList<Area>();
		for (Row rows : sheet) {
			//去除第一行
			if (rows.getRowNum()==0) {
				continue;
			}
			//去掉空行
			if (rows.getCell(0)== null || StringUtils.isBlank(rows.getCell(0).getStringCellValue())) {
				continue;
			}
			Area area = new Area();
			area.setId(rows.getCell(0).getStringCellValue());
			area.setProvince(rows.getCell(1).getStringCellValue());
			area.setCity(rows.getCell(2).getStringCellValue());
			area.setDistrict(rows.getCell(3).getStringCellValue());
			area.setPostcode(rows.getCell(4).getStringCellValue());
			//简写
			String province = area.getProvince(); //省
			String city = area.getCity(); //市
			String district = area.getDistrict(); //区
			//去掉最后一个字
			province = province.substring(0, province.length()-1);
			city = city.substring(0, city.length()-1);
			district=district.substring(0, district.length()-1);
			
			String[] head = PinYin4jUtils.getHeadByString((province+city+district));
			StringBuffer buffer = new StringBuffer();
			for (String headstr : head) {
				buffer.append(headstr);
			}
			String shortcode = buffer.toString();
			area.setShortcode(shortcode);
			String citycode = PinYin4jUtils.hanziToPinyin(city+"");
			area.setCitycode(citycode);
			
			areas.add(area);
			
		}
		areaService.save(areas);
		
		return NONE;
	}
	//分页查询
	
	@Action(value="findByPageAction" , results={@Result(name="success",type="json")})
	public String findByPage(){
		Pageable pageable = new PageRequest(page-1, rows);
		Specification<Area> specification = new Specification<Area>() {
			List<Predicate> list = new ArrayList<Predicate>();
			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				if (StringUtils.isNotBlank(model.getProvince())) {
					Predicate p1 = cb.like(root.get("province").as(String.class), "%"+model.getProvince()+"%");
					list.add(p1);
				}
				if (StringUtils.isNotBlank(model.getCity())) {
					Predicate p2 = cb.like(root.get("city").as(String.class), "%"+model.getCity()+"%");
					list.add(p2);
				}
				if (StringUtils.isNotBlank(model.getDistrict())) {
					Predicate p3 = cb.like(root.get("district").as(String.class), "%"+model.getDistrict()+"%");
					list.add(p3);
				}
				
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<Area> list = areaService.findByPage(specification , pageable );
		
		pushToValueStack(list);
		
		return SUCCESS;
	}
	
	
	@Action(value="areaSaveAction",results={@Result(name="success",location="./pages/base/area.html",type="redirect")})
	public String saveArea(){
		System.out.println(11111);
		areaService.saveArea(model);
		return SUCCESS;
	}
	
	
	
	
	
	
	
	
	
	
	
	

}
